package com.graduation.service;

import com.graduation.dao.GoodsDao;
import com.graduation.entity.Goods;
import com.graduation.entity.Recommend;
import com.graduation.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;

    @Override
    public Integer addGoods(Goods goods) {
        goods.setIssuetime(DateUtil.getCurrentDate());
        Integer result = goodsDao.addGoods(goods);
        return result;
    }

    @Override
    public Integer queryGoodsIsExists(String goodsType) {
        Integer er = goodsDao.queryGoodsIsExists(goodsType);
        return er;
    }

    @Override
    public Integer insertGoods(String goodsType, String fruitName) {
        return goodsDao.insertGoods(goodsType, fruitName);
    }

    @Override
    public List<Recommend> queryGoodsRecommend() {
        int randomNum = 0;
        List<Integer> list = goodsDao.queryGoodsId();
        List<Integer> arr = new ArrayList<>();
        //生成随机数
        Random random = new Random();
        if (list.size() < 8) {
            for (int i = 0; i < list.size(); i++) {
                randomNum = random.nextInt(list.size());
                arr.add(list.get(randomNum));
            }
        } else {
            for (int i = 0; i < 8; i++) {
                randomNum = random.nextInt(list.size());
                arr.add(list.get(randomNum));
            }
        }
        List<Recommend> goods = goodsDao.queryGoodsRecommend(arr);
        return goods;
    }

    @Override
    public Recommend queryRecommendById(String id) {
        return goodsDao.queryRecommendById(id);
    }

    @Override
    public List<Recommend> queryNewUpGoods() {
        return goodsDao.queryNewUpGoods();
    }

    @Override
    public List<Goods> showGoods() {
        List<Goods> goods = goodsDao.showGoods();
        return goods;
    }
}
